package com.aube.mis.service.impl;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Transformer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.aube.apiv2.service.app.theme.AppThemeVoService;
import com.aube.apiv2.vo.app.theme.RelateVo;
import com.aube.apiv2.vo.app.theme.ThemeDetailRespVo;
import com.aube.apiv2.vo.app.theme.ThemeVo;
import com.aube.mis.dto.VideoThemeDTO;
import com.aube.mis.service.AbstractService;
import com.aube.mis.service.ThemeService;
import com.aube.support.ResultCode;

@Service
@SuppressWarnings("unchecked")
public class ThemeServiceImpl extends AbstractService implements ThemeService {

	private static Logger log = LoggerFactory.getLogger(ThemeServiceImpl.class);

	@Resource
	AppThemeVoService appThemeVoService;

	@Override
	public VideoThemeDTO getVideoThemeDetail(String themeId) {
		ResultCode<ThemeDetailRespVo> themeDetailRespVo = appThemeVoService.findThemeDetailByThemeId(Long.parseLong(themeId));
		return convertVideoThemeDTO(themeDetailRespVo.getRetval());
	}

	@Override
	public VideoThemeDTO getShowDefaultTheme(String showId) {
		List<VideoThemeDTO> videoThemeList = getVideoThemeList(showId);
		for (VideoThemeDTO videoThemeDTO : videoThemeList) {
			if (videoThemeDTO.getIsDefault().equals("Y")) {
				return videoThemeDTO;
			}
		}
		return null;
	}

	@Override
	public VideoThemeDTO getVideoThemeDetailByVideo(String showId, String videoId) {
		ResultCode<ThemeDetailRespVo> themeDetailRespVo = appThemeVoService.findThemeDetailByVideoid(Long.parseLong(showId), Long.parseLong(videoId));
		return convertVideoThemeDTO(themeDetailRespVo.getRetval());
	}

	@Override
	public List<VideoThemeDTO> getVideoThemeList(String showId) {
		ResultCode<List<ThemeVo>> themeVoList = appThemeVoService.findThemeVoListByShowId(Long.parseLong(showId), null);
		List<VideoThemeDTO> videoList = (List<VideoThemeDTO>) CollectionUtils.collect(themeVoList.getRetval(), new Transformer() {

			@Override
			public VideoThemeDTO transform(Object object) {
				ThemeVo vo = (ThemeVo) object;
				VideoThemeDTO result = new VideoThemeDTO();
				result.setPicUrl(vo.getPicurl());
				result.setThemeId(vo.getId().toString());
				result.setThemeName(vo.getThemeName());
				result.setIsDefault("default".equals(vo.getThemeAttr()) ? "Y" : "N");
				result.setThemeDesc(vo.getThemeDesc());
				result.setThemeCode(vo.getThemeCode());
				return result;
			}
		});
		return videoList;
	}

	@Override
	public void updateVideoTheme(String videoId, String themeId) {
		appThemeVoService.saveThemeVoByVideoid(Long.parseLong(themeId), Long.parseLong(videoId));
	}

	@Override
	public void updateDefaultShowTheme(String showId, String themeId) {
		appThemeVoService.saveDefaultThemeVoByShowid(Long.parseLong(themeId), Long.parseLong(showId));
	}

	private VideoThemeDTO convertVideoThemeDTO(ThemeDetailRespVo vo) {
		VideoThemeDTO result = new VideoThemeDTO();
		result.setPicUrl(vo.getPicurl());
		result.setThemeCode(vo.getThemeCode());
		result.setThemeId(vo.getId().toString());
		result.setThemeName(vo.getThemeName());
		result.setPositionMode(vo.getPositionMode());
		result.setIsDefault("default".equals(vo.getThemeAttr()) ? "Y" : "N");
		result.setPreviewPicUrlList(vo.getPreviewPicList());
		result.setThemeDesc(vo.getThemeDesc());
		Map<String, String> animationMap = new LinkedHashMap<String, String>();
		List<String> relativePositionList = new ArrayList<String>();
		List<RelateVo> relativePositionVoList = vo.getRelativePositionList();
		if(CollectionUtils.isNotEmpty(relativePositionVoList)) {
			for (RelateVo relateVo : relativePositionVoList) {
				relativePositionList.add(relateVo.getKey());
				if ("Y".equals(relateVo.getDefaultFlg())) {
					result.setDefaultRelativePosition(relateVo.getKey());
				}
			}
			result.setRelativePositionList(relativePositionList);
		}
		List<RelateVo> animationEffectsList = vo.getAnimationEffectsList();
		if(CollectionUtils.isNotEmpty(animationEffectsList)) {
			for (RelateVo animationEffect : animationEffectsList) {
				animationMap.put(animationEffect.getKey(), animationEffect.getValue());
				if ("Y".equals(animationEffect.getDefaultFlg())) {
					result.setDefaultAnimationEffects(animationEffect.getKey());
				}
			}
			result.setAnimationEffectsList(animationMap);
		}
		return result;
	}
}
